var Article = require('../model_proxy').Article;
var logger = require('../logger');

var mod = {
    add: function(req, res) {
        var info = req.body;
        console.log('article-info---', info);
        Article.add({
            title: info.title,
            content: info.content,
            user_id: req.session.user._id,
        }, function(err, data) {
            console.log('err:::', err);
            console.log('data::::', data);
            if(err) {
                console.error('login error::', err);
            }
            
            if(data.length != 0) {
                logger.info('用户：' + req.session.user.userName + '(' + req.session.user.role + ')发布文章成功！');
                res.json({
                    status: 'success',
                    // data: data[0],
                    message: '发布成功！'
                });
            } else {
                res.json({
                    status: 'fail',
                    message: '发布失败！'
                });
            }
            
        })
    },

    getArticleByUserId(req, res) {
        console.log('req-userId---', req.session.user._id);
        
        Article.getArticleByUserId(req.session.user._id, function(err, data) {
            if(err) {
                res.json({
                    status: 'fail',
                });
            } else {
                res.json({
                    status: 'success',
                    data: data,
                });
            }

            
        });
        
    },

    delete: function(req, res) {
        console.log(req.body);
        Article.delete(req.body.articleId, function(err, data) {
            console.log('err::', err);
            console.log('data::', data);
            res.json({
                statue: 'success'
            });
        });
       
    },

    register: function() {
        User.register();
    }
}

module.exports = mod;